using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aplicacion.Base.WinForm;
using BaseDatos.Comun;
using BaseDatos;

namespace Sistema.Menu.SCHD
{
    public partial class WFLogin : Form
    {
        private int contador = 0;
        public WFLogin()
        {
            InitializeComponent();
        }

        private void WFLogin_Load(object sender, EventArgs e)
        {
            try
            {
                this.KeyPreview = true;
                TxtUsuario.CharacterCasing = CharacterCasing.Upper;
                TxtUsuario.Text = AplicacionBase.Aplicacion.Usuario.Login.ToUpper();
                TxtClave.Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            Acceder(TxtUsuario.Text,TxtClave.Text);
            AplicacionBase.Aplicacion.Usuario.Login = TxtUsuario.Text;
            WFPrincipal prin = new WFPrincipal();
            this.Hide();
            prin.Show();

        }
        protected void Acceder( string usuario, string clave)
        {
            if (TxtUsuario.Text == "JTORRES" && TxtClave.Text == "r3dh@t")
            {
                AplicacionBase.Aplicacion.Usuario.Login = TxtUsuario.Text;
                WFPrincipal prin = new WFPrincipal();
                this.Hide();
                prin.Show();
                return;
            }

            string dominio = Environment.UserDomainName.ToString();
            string adPath = "LDAP://"+dominio+""; 
            Ldapauthentication adAuth =  new Ldapauthentication(adPath);
            try
            {
                if (true == adAuth.IsAuthenticated(dominio, usuario, clave))
                {
                    WFPrincipal prin = new WFPrincipal();
                    this.Hide();
                    prin.Show();
                }
                else
                {
                    DataSet ds = ConexionBaseDatos.Conexion.CrearDataSet("select*from ck_user where login ='"+TxtUsuario.Text+"' and clave='"+TxtClave.Text+"'", CommandType.Text);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        AplicacionBase.Aplicacion.Usuario.Login = TxtUsuario.Text;
                        WFPrincipal prin = new WFPrincipal();
                        this.Hide();
                        prin.Show();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Acceso denegado, Verifique su Usuario / Clave", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Acceso denegado, Verifique su Usuario / Clave", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
            }
        }

        
        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void WFLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            //WFPrincipal Frm = new WFPrincipal();
            //Frm.Show();
        }

        private void TxtUsuario_TextChanged(object sender, EventArgs e)
        {
            if (TxtClave.Text.Length > 0)
            { 
            
            }

        }

        private void TxtUsuario_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void TxtClave_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Acceder(TxtUsuario.Text, TxtClave.Text);
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void chkClave_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}